<table class="admingrid" id="companyupload">
</table>
<div id="companyupload_pager"></div>

<table>
<tr><td valign="top">
<form action="index.php?mode=adminservice&page=maintenance&service=upload" name="companyupload_form" method="post" target="_background" enctype="multipart/form-data">
    <input type="file" name="company_import">
    <input type="hidden" name="MAX_FILE_SIZE" value="9999999999999999">
</form>
</td><td valign="top">
Select uploaded companies type
<select name="companytype_imp">
    <option value="1">Export company</option>
    <option value="2">Import company</option>
    <option value="3">Hungarian company abroad export</option>
    <option value="4">Hungarian company abroad import</option>
    <option value="5">Recommended service</option>
    <option value="6">Business lead</option>
</select>
</td></tr>
</table>

<button onclick='startCompanyUpload()'>Upload companies</button>
<button onclick="finalize()" id="finalize">Finalize import</button>
<button onclick="rollback()" id="rollback">Rollback import</button>

<script>
grids.companyupload = {
    url : generateServiceUrl({
        admin : true,
        service : "getuploadedcompanies",
        page : "maintenance"
    }),
    colNames : [
        "Company name",
        "Address",
        "Contact email",
        "Contact person",
        "Homepage (en)",
        "Homepage (hu)"
    ],
    colModel : [
        { name : "company_name",  index : "company_name",  width : 150 },
        { name : "address",       index : "address",       width : 150 },
        { name : "email",         index : "email",         width : 150 },
        { name : "contactperson", index : "contactperson", width : 100 },
        { name : "hpenglish",     index : "hpenglish",     width : 100 },
        { name : "hphungarian",   index : "hphungarian",   width : 100 }
    ],
    caption : "Uploaded companies"
};
var uploadFlow = new Flow([
    userFunction("Uploading selected file", function(params) {
        jQuery("form[name='companyupload_form']").get(0).submit();
        return true;
    }, { }),
    adminService("Validating uploaded companies", "maintenance", "validatecompanies", function() { return { type : jQuery("select[name='companytype_imp'] option:selected").val() } } ),
    adminService("Parsing companies into grid",   "maintenance", "inserttmp", function() { return { type : jQuery("select[name='companytype_imp'] option:selected").val()  } } )
]);
uploadFlow.onBeforeStart(function() {
    return true;
});
uploadFlow.onAfterFinish(function() {
    refreshGrid("companyupload");
});
uploadFlow.defaultMsg = "Uploading selected file";

function startCompanyUpload() {
    selectedValue = jQuery("select[name='companytype_imp'] option:selected").val();
    uploadFlow.executeFlow();
}

function finalize() {
    callAdminService("maintenance", "finalize",{}, {
        success : function(result) {
            displayMessage("info", "Companies has been successfully imported");
            refreshGrid("companyupload");
        }
    });
}

function rollback() {
    callAdminService("maintenance", "rollback",{}, {
        success : function(result) {
            displayMessage("info", "Import has been successfully rollbacked");
            refreshGrid("companyupload");
        }
    });
}

</script>
